import { Body, Controller, Get, Post } from "@nestjs/common";
import { BreathServerService } from "./breath-server.service";
import { MapRes } from "../../system-server/src/types/types";

@Controller()
export class BreathServerController {
  constructor(private readonly breathServerService: BreathServerService) {}

  @Get()
  getHello(): any {
    return MapRes.success();
  }

  @Post("/testOpen")
  async testOpenBreathServer(@Body() params: Record<string, any>) {
    this.breathServerService.modifyFilter({}, true);
    return await this.breathServerService.connectBreathServer();
  }

  @Post("/open")
  async openBreathServer(@Body() params: Record<string, any>) {
    this.breathServerService.modifyFilter(
      {
        low: params.low,
        hi: params.hi,
        order: params.order,
        yCenter: params.yCenter,
        peak_threshold: params.peakThreshold,
        valley_threshold: params.valleyThreshold,
        prominence_threshold: params.prominenceThreshold,
        min_distance: params.minDistance,
      },
      true,
    );
    return await this.breathServerService.connectBreathServer();
  }

  @Post("/close")
  closeBreathServer() {
    return this.breathServerService.close();
  }

  @Post("/getRecent")
  async getRecent() {
    return await this.breathServerService.getRecent();
  }

  @Get("/record/start")
  recordStart() {
    return this.breathServerService.recordStart();
  }

  @Get("scan/start")
  scanStart() {
    return this.breathServerService.scanStart();
  }

  @Get("scan/end")
  scanEnd() {
    return this.breathServerService.scanEnd();
  }

  @Get("/record/end")
  recordEnd() {
    return this.breathServerService.recordEnd();
  }

  @Post("/setAmplitude")
  setAmplitude(@Body() params: Record<string, any>) {
    const amplitude = params.amplitude;
    this.breathServerService.setAmplitude(amplitude);
    return MapRes.success();
  }

  @Get("/breathInfo")
  getBreathInfo() {
    return this.breathServerService.getBreathInfo();
  }

  @Post("/setFilter")
  setFilter(@Body() params: Record<string, any>) {
    this.breathServerService.modifyFilter(
      {
        low: params.low,
        hi: params.hi,
        order: params.order,
        yCenter: params.yCenter,
        peak_threshold: params.peakThreshold,
        valley_threshold: params.valleyThreshold,
        prominence_threshold: params.prominenceThreshold,
        min_distance: params.minDistance,
      },
      false,
    );
    return {
      code: 1,
    };
  }
}
